package reduce.fz.com.reduce.base;

import android.content.Context;
import android.os.Environment;


import java.io.File;

import reduce.fz.com.reduce.util.ReduceLog;

/**
 * 文件缓存处理
 */
public class FileCache {

    public static void init(Context context) {
        mkDirs(getImageCacheDir(context));
        mkDirs(getDataCacheDir(context));
        mkDirs(getPhotoDir(context));
        mkDirs(getCropDir(context));
        mkDirs(getStickerDir(context));

        //每次进入系统清理一下剪裁缓存
        try{
            File[] imgCachedFiles = new File(getCropDir(context)).listFiles();
            if (imgCachedFiles != null){
                for (File f : imgCachedFiles) {
                    f.delete();
                }
            }
        }catch(Exception e){
            ReduceLog.e(e);
        }
    }


    public static void mkDirs(String dirPath) {
        File file = new File(dirPath);
        if (!file.exists()) file.mkdirs();
    }

    public static String getImageCacheDir(Context context) {
        return getRootDirectory(context) + "/image/";
    }

    public static String getDataCacheDir(Context context) {
        return getRootDirectory(context) + "/data/";
    }

    public static String getPhotoDir(Context context) {
        return getRootDirectory(context) + "/photo/";
    }

    /**
     * 剪裁的存储路径一般都是临时的，每次进入启动feel清理一次
     * @param context
     * @return
     */
    public static String getCropDir(Context context) {
        return getRootDirectory(context) + "/crop/";
    }

    /**
     * 贴纸存放路径
     * @param context
     * @return
     */
    public static String getStickerDir(Context context) {
        return getRootDirectory(context) + "/sticker/";
    }

//    public static String getTempCacheDir() {
//        return getSDCardDirectory() + "/Feel/";
//    }
//
//    public static String getSDCardDirectory() {
//        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
//            return Environment.getExternalStorageDirectory().getPath();
//        }
//        return null;
//    }

    public static String getRootDirectory(Context context) {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            final String cacheDir = "/Android/data/" + context.getPackageName();
            return Environment.getExternalStorageDirectory() + cacheDir;
        } else {
            String path = null;
            File cacheDir = context.getCacheDir();
            if (cacheDir.exists()) path = cacheDir.getAbsolutePath();
            return path;
        }
    }
}
